/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/
document.domain = DOMAIN_SUFFIX;

KindEditor.plugin('playimage', function(K) {
	var self = this, name = 'playimage',lang = self.lang(name + '.');
	self.plugin.playimage = {
		edit: function () {
			var time = (new Date()).valueOf();
			var eid='p'+time;

			var html = [
				'<div class="playimage" style="padding:20px;">',
				'<ul class="ke-img-list">',
				'<li id="'+eid+'">',
				'<div class="pic"><img src="'+PUBLIC+'/Admin/images/upload.gif"/><input class="playimgupload" data-img="'+eid+'" type="file" name="files[]"><input type="hidden" name="pic[]" value=""/></div>',
				'<div class="param"><textarea name="con[]"></textarea>',
				'<a href="javascript:;" class=" del">X</a></div>',
				'</li>',
				'</ul>',
				'<div><span class="ke-button-common ke-button-outer"><input type="button" class="ke-button-common ke-button" name="addnew" value="' + lang.add + '" /></span></div>',
				'</div>'
			].join('');

			var dialog = self.createDialog({
				name : name,
				width : 600,
				height : 400,
				title : self.lang(name),
				body : html,
				yesBtn : {
					name : self.lang('yes'),
					click : function(e) {
						var sdata=[];

						$('.ke-img-list li').each(function(){
							var pic=$(this).find('[name="pic[]"]').val();
							var con=$(this).find('[name="con[]"]').val();
							sdata.push({pic:pic,con:con});
						});

						self.exec('playimage', JSON.stringify(sdata)).hideDialog().focus();
					}
				},
				beforeRemove : function() {

				}
			});
			var div = dialog.div,
				imglist = K('.ke-img-list', div),
				addnew = K('[name="addnew"]', div);
			addnew.click(function(){
				createli('','');
			});

			var createli=function(pic,con){
				var time = (new Date()).valueOf();
				var eid='p'+time;
				var img=PUBLIC+'/Admin/images/upload.gif';
				if(pic){
					img='http://img.chuguofeng.com/Admin/Editor/'+pic;
				}
				var li='<li id="'+eid+'">'
					+'<div class="pic"><img src="'+img+'"/><input class="playimgupload" data-img="'+eid+'" type="file" name="files[]"><input type="hidden" name="pic[]" value="'+pic+'"/></div>'
					+'<div class="param"><textarea name="con[]">'+con+'</textarea>'
					+'<a href="javascript:;" class=" del">X</a></div>'
					+'</li>';
				imglist.append(li);
				upload();
			}

			var upload=function(){
				$('.ke-img-list li').each(function(){
					$(this).find('[type="file"]').fupload({path:'ZWRpdG9y',input:'pic',max:1,imgType:'big',dev:'playimg'});
				});
			};
			upload();

			$('.ke-img-list').delegate('.del','click',function(){
				if($('.ke-img-list li').length>1){
					$(this).closest('li').fadeOut(function(){
						$(this).remove();
					})
				}
			});

			self.cmd.selection();
			var a = self.plugin.getSelectedPlayimage();
			if (a) {
				self.cmd.range.selectNode(a[0]);
				self.cmd.select();
				var result= $.trim($(a).siblings('textarea').val());
				result=$.parseJSON(result);
				$('.ke-img-list').html('');
				$.each(result,function(i,j){
					createli(j.pic, j.con);
				});
			}
		},
		'delete' : function() {
			self.exec('unlink', null);
		}
	}
	self.clickToolbar(name, self.plugin.playimage.edit);

});
